package com.tedu.redis.controller;


import com.tedu.redis.dto.Notice;
import com.tedu.redis.mapper.NoticeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/notice/")
public class NoticeController {
    public static final String NOTICE_KEY="NOTICE_KEY_";
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("selectbyid")
    Notice findNoticeByid(Long id){

        ValueOperations valueOperations= redisTemplate.opsForValue();
        Notice notice =(Notice) valueOperations.get("NOTICE_KEY_" + id);
        if(notice!=null){
            return notice;
        }
        notice = noticeMapper.selectNoticeById(id);
        valueOperations.set("NOTICE_KEY_"+id,notice);
        log.debug("Date from mysql********************************************");
        return notice;
    }
}
